package com.wd.live

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.tabs.TabLayout
import com.wd.live.databinding.ActivityMainBinding
import com.wd.live.module_live.LiveFragment
import com.wd.live.module_my.MyFragment
import com.wd.live.module_nearby.NearbyFragment
import com.wd.live.module_ranking.RankingFragment
import com.wd.live.module_wide.WideFragment
import com.wd.module_base.ui.BaseActivity
import com.wd.module_base.ui.BaseViewModel
/*
* 项目搭建: 组件化指的是将应用程序拆分成不同的功能模块，每个模块可以独立开发、集成和测试。MVVM 是一种架构模式，
* 它将应用程序分为视图、数据绑定和业务逻辑层。BaseActivity 和 BaseFragment 是基类，用于提供应用程序中所有
*  Activity 和 Fragment 的共同行为和属性。*/
@Route(path = "/app/main")
class MainActivity : BaseActivity<BaseViewModel,ActivityMainBinding>() {
    val image= mutableListOf<Int>(R.mipmap.app_square_0,R.mipmap.app_position_page_0,R.mipmap.app_broadcast_icon,R.mipmap.app_list_selected_0,R.mipmap.app_my_choice_0)
    val defaultImage= mutableListOf<Int>(R.mipmap.app_square,R.mipmap.app_position_page_0,R.mipmap.app_broadcast_icon,R.mipmap.app_list_selected_0,R.mipmap.app_my_choice_0)
    val selectImage= mutableListOf<Int>(R.mipmap.app_square,R.mipmap.app_position_page,R.mipmap.app_broadcast_icon,R.mipmap.app_list_selected,R.mipmap.app_my_choice)
    val titleArr= mutableListOf<String>("广场","附近","","榜单","我的")
    override fun getLayout(): Int {
        return R.layout.activity_main
    }

    override fun initData() {
        val sharedPreferences = getSharedPreferences("user", MODE_PRIVATE)
        val edit = sharedPreferences.edit()
        edit.putBoolean("flag",false)
        edit.commit()
        if (!sharedPreferences.getBoolean("flag",false)){
            ARouter.getInstance().build("/module_login/home").navigation()

        }else{

        }

        val fragment= mutableListOf(WideFragment(),NearbyFragment(),LiveFragment(),RankingFragment(),MyFragment())
        binding.vpId.adapter=object :FragmentPagerAdapter(supportFragmentManager){
            override fun getPageTitle(position: Int): CharSequence? {
                return titleArr[position]
            }

            override fun getCount(): Int {
               return fragment.size
            }

            override fun getItem(position: Int): Fragment {
                return fragment[position]
            }
        }

        binding.tbId.setupWithViewPager(binding.vpId)
        for ((index, i) in defaultImage.withIndex()) {
            binding.tbId.getTabAt(index)?.setIcon(defaultImage[index])
        }
        binding.tbId.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                if (tab != null) {
                    tab.setIcon(selectImage[tab.position])
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                if (tab != null) {
                    tab.setIcon(image[tab.position])
                }
            }
        })



    }
    override fun getViewModelClass(): Class<BaseViewModel> {
        return BaseViewModel::class.java
    }

}